<?php
namespace app\api;
use think\Cache;
use think\Model;
use think\Request;

class BaseModel extends Model
{
    public $request;
    public $requestData;
    protected $user;
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->request = Request::instance();
        $this->requestData = empty($data)?$this->request->param(''):$data;

        if($this->request->__get('user')){
            $this->user = $this->request->__get('user');
        }

        if(isset($this->ParameterDetection)){
            call_user_func([$this, 'checkData']);
        }
    }

    /**
     * 路由检测以及参数是否已传
     */
    public function checkData()
    {
        $path_info = $_SERVER['PATH_INFO'];
        
        $path_info_result = substr($path_info,0,1);
        //兼容linux系统
        if($path_info_result == '/')$path_info = substr($path_info, 1);

        $path = explode('/', $path_info);

        if(count($path) > 3)die(json_encode(['data'=>[],'code'=>'-1','message'=>'地址不合法']));
        if(isset($this->ParameterDetection[$path[2]])){
            foreach($this->ParameterDetection[$path[2]] as $k => $v){
                if(!array_key_exists($v,$this->requestData))die(json_encode(['data'=>[],'code'=>'-1','message'=>$v.'参数未传']));
            }
        }
    }


    /**
    *   上传图片方法
    */
    public function update_img($input_image=''){
        if($input_image == '')return $input_image;
        $file = request()->file($input_image);
        if($file){
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/');
            if ($info) {
                return $info;
            } else {
                return '';
            }
        }else{
            return '';
        }
    }

    /**
    *   循环调用图片方法
    */
    public function update_array_img($input_image=''){
        if($input_image == '')return $input_image;
        $file = request()->file($input_image);

        if($file){
            foreach ($file as $key => $value) {
                $info = $value->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/');
                if ($info) {
                    $img[] = str_replace('\\','/',$info->getSaveName());
                    //$img[] =
                    //return $info;
                } 
            }
            if(isset($img)){
                return $img;
            }else{
                return null;
            }
        }else{
            return '';
        }
    }


    /**
    *   上传图片方法
    */
    public function update_circle_img($input_image=''){
        if($input_image == '')return $input_image;
        $file = request()->file($input_image);

        if($file){
            $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public/uploads/');
            
            if ($info) {
                $name = str_replace("\\","/",$info->getSaveName());

                return $name;
            } else {
                return '';
            }
        }else{
            return '';
        }
    }

    /**
    *   上传视频方法
    */
    public function update_video($input_video=''){
        if($input_image == '')return $input_image;
        $file = request()->file($input_image);

        if($file){
            $info = $file->validate(['ext'=>'mp4'])->move(ROOT_PATH . 'public/uploads/video');
            if ($info) {
                $name = str_replace("\\","/",$info->getSaveName());

                return $name;
            } else {
                return '';
            }
        }else{
            return '';
        }
    }

    /**
    *   上传音频方法
    */
    public function update_music($input_music=''){
        if($input_image == '')return $input_image;
        $file = request()->file($input_image);

        if($file){
            $info = $file->validate(['ext'=>'mp3'])->move(ROOT_PATH . 'public/uploads/music');
            if ($info) {
                $name = str_replace("\\","/",$info->getSaveName());

                return $name;
            } else {
                return '';
            }
        }else{
            return '';
        }
    }

    /**
     * 友好时间显示
     * @param $time
     * @return bool|string
     */

    function friend_date($time){
        $rtime = date ( "m月d日", $time );
        $htime = date ( "H:i", $time );
        
        $time = time () - $time;
        
        if ($time < 60) {
            $str = '刚刚';
        } elseif ($time < 60 * 60) {
            $min = floor ( $time / 60 );
            $str = $min . '分钟前';
        } elseif ($time < 60 * 60 * 24) {
            $h = floor ( $time / (60 * 60) );
            $str = $h . '小时前 ' . $htime;
        } elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor ( $time / (60 * 60 * 24) );
            if ($d == 1)
                $str = '昨天 ' . $rtime;
            else
                $str = '前天 ' . $rtime;
        } else {
            $str = $rtime;
        }
        return $str;  
    }
/*
    //多图
    public function upload(){
        // 获取表单上传文件
        $files = request()->file('image');
        foreach($files as $file){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                echo $info->getExtension(); 
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }    
        }
    }*/

    function redis(){

        $options = [

        'type'  => 'redis',//指定类型

        'password'=>'asdfghjkl',

        'prefix' => 'sbn-',

        'host'      => '127.0.0.1',

    ];

    Cache::init($options);//初始化

    }


}